package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.ApplicationContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.enity.AddressBook;
import com.itheima.reggie.service.AddressBookService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 地址簿管理
 */
@Slf4j//日志记录
@RestController //代表返回的是json格式的数据
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增
     */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook) {
        addressBook.setUserId((Long) ApplicationContext.getData());
        log.info("addressBook:{}", addressBook);
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    /**
     * 设置默认地址
     */
    @PutMapping("/default")       //JSON格式数据，使用AddressBook接受
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        //update address_book () set  isdefault=0  (条件) where user_id=?
        log.info("addressBook:{}", addressBook);//用日志记录addressBook数据
        //创建一个条件构造器
        LambdaUpdateWrapper<AddressBook> LambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        //进行条件对比    AddressBook::getUserId :  获取用户Id      ApplicationContext.getData() :  向Th中存储数据
        LambdaUpdateWrapper.eq(AddressBook::getUserId, ApplicationContext.getData());//where user_id=?
        //状态进行比较     默认地址的状态
        LambdaUpdateWrapper.set(AddressBook::getIsDefault, 0);//set  isdefault=0
        //SQL:update address_book set is_default = 0 where user_id = ?
        addressBookService.update(LambdaUpdateWrapper);
        //将默认地址的状态设置为1
        addressBook.setIsDefault(1);
        //SQL:update address_book set is_default = 1 where id = ?
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    /**
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    //@PathVariable : 获取到路径中的数据
    public R get(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return R.success(addressBook);
        } else {
            return R.error("没有找到改对象");
        }
    }

    /**
     * 查询默认地址
     */
    @GetMapping("/default")
    public R<AddressBook> getDefault() {
        //创建条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, ApplicationContext.getData());
        queryWrapper.eq(AddressBook::getIsDefault, 1);
        //SQL:select * from address_book where user_id = ? and is_default = 1
        //getOne : 是在通用service中定义的方法
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        if (addressBook == null) {
            return R.error("没有找到该对象");
        } else {
            return R.success(addressBook);
        }
    }

    /**
     * 查询指定用户的全部地址
     */
    @GetMapping("/list")
    public R list(AddressBook addressBook) {
        addressBook.setUserId((Long) ApplicationContext.getData());
        log.info("addressBook:{}", addressBook);
        //条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != addressBook.getUserId(), AddressBook::getUserId, addressBook.getUserId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);
        //SQL:select * from address_book where user_id = ? order by update_time desc
        //我用service层对象调用list方法根据构造器条件返回一个list集合
        return R.success(addressBookService.list(queryWrapper));
    }

    //删除地址
    @DeleteMapping
    public R deletes(Long[] ids) {
        List<Long> longs = Arrays.asList(ids);
        addressBookService.removeByIds(longs);
        return R.success("删除成功");
    }


    //修改地址
    @PutMapping
    public R update(@RequestBody AddressBook addressBook) {
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }
}
